package com.kinth.work.webmagic.huicong;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.log4j.Logger;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import com.alibaba.fastjson.JSONObject;
import com.kinth.common.HttpRequest;
import com.kinth.common.HttpUtil;
import com.kinth.rest.api.CreateEmailInfoRequest;

/**
 * 1. 暂时把product和introduction放到一起在remark字段里面。
 */
public class DataPipeline implements Pipeline {

	
	private Logger logger = Logger.getLogger(DataPipeline.class);
	
	public DataPipeline() {
		super();
	}
	
	public DataPipeline(Integer subTaskId) {
		super();
	}

	//如果传入的参数为空，就输出"暂无数据"
	public String forEmpty(String s)
	{
		if(s == null)
		{
			return "暂无数据";
		}
		return s;
	}
	public void process(ResultItems resultItems, Task task) {
		logger.info("Pipeline processes.");
		
		String forEmpty = "暂无数据";
		String name = resultItems.get("name");
		if(name == null)
		{
			return;
			//也就是如果公司名称为空则直接返回
			//name = forEmpty;
		}
			
		String product = resultItems.get("product");
		if(product == null)
		{
			product = forEmpty;
		}
		String address = resultItems.get("address");
		if(address == null)
		{
			address = forEmpty;
		}
		String people = resultItems.get("people");
		if(people == null)
		{
			people = forEmpty;
		}
		String qq = resultItems.get("qq");
		if(qq == null)
		{
		    qq = forEmpty;
		}else
		{
			qq = qq+"@qq.com";
		}
		String phone1 = resultItems.get("phone1");
		if(phone1 == null)
		{
			phone1 = forEmpty;
			
		}
		String phone2 = resultItems.get("phone2");
		if(phone2 == null)
		{
			phone2 = forEmpty;
		}
		String phone3 = resultItems.get("phone3");
		if(phone3 == null)
		{
			phone3 = forEmpty;
		}
		String introduction = resultItems.get("introduction");
		if(introduction == null)
		{
			introduction = forEmpty;
		}
		
		String industry = resultItems.get("industry");
		if(industry == null)
		{
			industry = forEmpty;
		}
		
		String source = resultItems.get("url");
		if(source == null)
		{
			source = forEmpty;
		}
		//后台传输接口
		ResteasyClient client = new ResteasyClientBuilder().build();
		try{
//			String requestString = "http://data.kinthtech.com/api/v1.0/company/spider-data-insert.json";

			CreateEmailInfoRequest createRequest = 
					new CreateEmailInfoRequest();
			createRequest.setAddress(address);
			createRequest.setCompany(name);
			createRequest.setEmail(qq);
			createRequest.setUsername(people);
//			createRequest.setRemark("公司产品: \n"+product+"\n|    公司简介:\n"+introduction);
			createRequest.setProduct(product);
			createRequest.setIntroduce(introduction);
			createRequest.setPhone1(phone1);
			createRequest.setPhone2(phone2);
			createRequest.setPhone3(phone3);
			createRequest.setIndustry(industry);
		    createRequest.setSource(source);
		    
		    HttpUtil.save(createRequest);
		    
		    //应该是phone这里的额字段太长了。
//		    System.out.println("TEST"+ JSONObject.toJSONString(createRequest) );
//		    HttpRequest.httpPost(requestString, JSONObject.parseObject(JSONObject.toJSONString(createRequest)));
		    
//			ResteasyWebTarget target = client.target(requestString);
//			Response response = target.request().post(Entity.json(createRequest));
//			//System.out.println("status: "+response.getStatusInfo());
//			//调用出错
//			if(response.getStatusInfo() != Status.OK){
//				
//				System.out.println("error!!!!!公司名重复");
//				//此处报错是因为表中已经有了这个公司名，而且是不止一个。两个的url地址不同。也不知道为嘛会插入两个。
////				System.out.println("传输失败之后：");
////				System.out.println("address: "+createRequest.getAddress());
////				System.out.println("area: "+createRequest.getArea());
////				System.out.println("company: "+createRequest.getCompany());
////				System.out.println("email: "+createRequest.getEmail());
////				System.out.println("industry: "+createRequest.getIndustry());
////				System.out.println("introduce: "+createRequest.getIntroduce());
////				System.out.println("phone1: "+createRequest.getPhone1());
////				System.out.println("phone2: "+createRequest.getPhone2());
////				System.out.println("phone3: "+createRequest.getPhone3());
////				System.out.println("product: "+createRequest.getProduct());
////				System.out.println("source: "+createRequest.getSource());
////				System.out.println("username: "+createRequest.getUsername());
////				System.out.println("url: "+createRequest.getUrl());
////				System.out.println("qq: "+createRequest.getEmail());
//			}
		}catch(Exception e)
		{
			System.out.println("DataPipeline: 传输有问题"+e.toString());
			e.printStackTrace();
		}
		finally{
			client.close();
			
		}
        
		logger.info("info:"+name+product+address+people+qq);
	}
	
	
}
